/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExJoViewPanel.java
 *
 * Created on 29/11/2011, 22:59:43
 */

package proj5labbd.graphics.exibicaoJornal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import proj5labbd.DBInterface;
import proj5labbd.graphics.abstractOpGraphics.OperationPanel;

public class ExJoViewPanel extends OperationPanel {

    /** Creates new form ExJoViewPanel */
    public ExJoViewPanel(ExJoFrame frame, DBInterface dbInterface) {
        super(frame, dbInterface);
        initComponents();
        jbtView.addActionListener(new ExJoAction());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnParam = new javax.swing.JPanel();
        jlbData = new javax.swing.JLabel();
        jlbHoraIn = new javax.swing.JLabel();
        jlbJornal = new javax.swing.JLabel();
        jlbFuncionario = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jlbIbope = new javax.swing.JLabel();
        jtxtIbope = new javax.swing.JTextField();
        jftxtDate = new javax.swing.JFormattedTextField();
        jftxtIni = new javax.swing.JFormattedTextField();
        jftxtFim = new javax.swing.JFormattedTextField();
        jtxtJornal = new javax.swing.JTextField();
        jtxtApresentador = new javax.swing.JTextField();
        jpnField = new javax.swing.JPanel();
        jchkData = new javax.swing.JCheckBox();
        jchkHoraIn = new javax.swing.JCheckBox();
        jchkHoraFim = new javax.swing.JCheckBox();
        jchkIbope = new javax.swing.JCheckBox();
        jchkJornal = new javax.swing.JCheckBox();
        jchkApresentador = new javax.swing.JCheckBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtbView = new javax.swing.JTable();
        jbtView = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Visualização"));

        jpnParam.setBorder(javax.swing.BorderFactory.createTitledBorder("Parâmetros de busca"));

        jlbData.setText("Data");

        jlbHoraIn.setText("Hora de início");

        jlbJornal.setText("Jornal");

        jlbFuncionario.setText("Apresentador");

        jLabel1.setText("Hora de término");

        jlbIbope.setText("Ibope");

        try {
            jftxtDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##-##-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtDate.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        try {
            jftxtIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtIni.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        try {
            jftxtFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtFim.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        javax.swing.GroupLayout jpnParamLayout = new javax.swing.GroupLayout(jpnParam);
        jpnParam.setLayout(jpnParamLayout);
        jpnParamLayout.setHorizontalGroup(
            jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnParamLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnParamLayout.createSequentialGroup()
                        .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlbData, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jftxtDate, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jftxtIni)
                            .addComponent(jlbHoraIn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jftxtFim)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtIbope, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlbIbope, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)))
                    .addGroup(jpnParamLayout.createSequentialGroup()
                        .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtJornal, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlbJornal))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlbFuncionario)
                            .addComponent(jtxtApresentador, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jpnParamLayout.setVerticalGroup(
            jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnParamLayout.createSequentialGroup()
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbData)
                    .addComponent(jlbHoraIn)
                    .addComponent(jLabel1)
                    .addComponent(jlbIbope))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtIbope, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jftxtDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jftxtIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jftxtFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbJornal)
                    .addComponent(jlbFuncionario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnParamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtJornal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtApresentador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        jpnField.setBorder(javax.swing.BorderFactory.createTitledBorder("Campos a selecionar"));

        jchkData.setText("Data");

        jchkHoraIn.setText("Hora de início");

        jchkHoraFim.setText("Hora de término");

        jchkIbope.setText("Ibope");

        jchkJornal.setText("Jornal");

        jchkApresentador.setText("Apresentador");

        javax.swing.GroupLayout jpnFieldLayout = new javax.swing.GroupLayout(jpnField);
        jpnField.setLayout(jpnFieldLayout);
        jpnFieldLayout.setHorizontalGroup(
            jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jchkData)
                    .addComponent(jchkHoraIn)
                    .addComponent(jchkHoraFim)
                    .addComponent(jchkIbope)
                    .addComponent(jchkJornal)
                    .addComponent(jchkApresentador))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jpnFieldLayout.setVerticalGroup(
            jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldLayout.createSequentialGroup()
                .addComponent(jchkData)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkHoraIn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkHoraFim)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkIbope)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkJornal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkApresentador)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jtbView.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane3.setViewportView(jtbView);

        jbtView.setText("Visualizar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jpnParam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jpnField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jbtView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jpnParam, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jpnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtView))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton jbtView;
    private javax.swing.JCheckBox jchkApresentador;
    private javax.swing.JCheckBox jchkData;
    private javax.swing.JCheckBox jchkHoraFim;
    private javax.swing.JCheckBox jchkHoraIn;
    private javax.swing.JCheckBox jchkIbope;
    private javax.swing.JCheckBox jchkJornal;
    private javax.swing.JFormattedTextField jftxtDate;
    private javax.swing.JFormattedTextField jftxtFim;
    private javax.swing.JFormattedTextField jftxtIni;
    private javax.swing.JLabel jlbData;
    private javax.swing.JLabel jlbFuncionario;
    private javax.swing.JLabel jlbHoraIn;
    private javax.swing.JLabel jlbIbope;
    private javax.swing.JLabel jlbJornal;
    private javax.swing.JPanel jpnField;
    private javax.swing.JPanel jpnParam;
    private javax.swing.JTable jtbView;
    private javax.swing.JTextField jtxtApresentador;
    private javax.swing.JTextField jtxtIbope;
    private javax.swing.JTextField jtxtJornal;
    // End of variables declaration//GEN-END:variables

    class ExJoAction implements ActionListener {
        
        /*
         * Controla os eventos de visualização
         */
        public void actionPerformed(ActionEvent e) {
            
            if (e.getSource() == jbtView) {
                
                ArrayList<String> param = new ArrayList<String>();
                ArrayList<String> field = new ArrayList<String>();
                ArrayList<String> tables = new ArrayList<String>();
                tables.add("exibicaoJornal");
                
                if (jftxtDate.getText().compareTo("  -  -    ") != 0) {
                    param.add("exibicaoJornal.dataExJo="+"TO_DATE('"+jftxtDate.getText()+"','DD-MM-YYYY')");
                }
                if (jftxtIni.getText().compareTo("  :  ") != 0) {
                    param.add("exibicaoJornal.horaInicioExJo='"+jftxtIni.getText()+"'");
                }
                if (jftxtFim.getText().compareTo("  :  ") != 0) {
                    param.add("exibicaoJornal.horaFimExJo='"+jftxtFim.getText()+"'");
                }
                if (jtxtJornal.getText().trim().compareTo("") != 0) {
                   try {
                        String[][] s = dbInterface.view(new String[]{"idPr"}, 
                                new String[]{"UPPER(jornal.tituloPr)=UPPER('" + jtxtJornal.getText() + "')"},
                                new String[]{"jornal"});
                        param.add("exibicaoJornal.idPr="+s[0][0]);
                    } catch (SQLException ex) {
                        Logger.getLogger(ExJoRmPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (jtxtApresentador.getText().trim().compareTo("") != 0) {
                    try {
                        String[][] s = dbInterface.view(new String[]{"idFu"}, 
                                new String[]{"UPPER(funcionario.nomeCompletoFu)=UPPER('" + jtxtApresentador.getText() + "')"},
                                new String[]{"funcionario"});
                        param.add("exibicaoJornal.idFu="+s[0][0]);
                    } catch (SQLException ex) {
                        Logger.getLogger(ExJoRmPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (jtxtIbope.getText().trim().compareTo("") != 0) {
                    param.add("exibicaoJornal.ibopeExJo="+"TO_NUMBER('"+jtxtIbope.getText()+"')");
                }
                
                if (jchkApresentador.isSelected()) {
                    field.add("funcionario.nomeCompletoFu");
                    tables.add("funcionario");
                    param.add("funcionario.idFu=exibicaoJornal.idFu");
                }
                if (jchkData.isSelected()) {
                    field.add("exibicaoJornal.dataExJo");
                }
                if (jchkJornal.isSelected()) {
                    field.add("jornal.tituloPr");
                    tables.add("jornal");
                    param.add("jornal.idPr=exibicaoJornal.idPr");
                }
                if (jchkHoraIn.isSelected()) {
                    field.add("exibicaoJornal.horaInicioExJo");
                }
                if (jchkHoraFim.isSelected()) {
                    field.add("exibicaoJornal.horaFimExJo");
                }
                if (jchkIbope.isSelected()) {
                    field.add("exibicaoJornal.ibopeExJo");
                }
                
                String paramArray[] = new String[param.size()];
                String fieldArray[] = new String[field.size()];
                String tableArray[] = new String[tables.size()];

                param.toArray(paramArray);
                field.toArray(fieldArray);
                tables.toArray(tableArray);
                
                try {
                    String[][] s = dbInterface.view(fieldArray, paramArray, tableArray);
                    if (fieldArray.length == 0) {
                        fieldArray = new String[6];
                        fieldArray[0] = "ID Jornal";
                        fieldArray[1] = "ID Apresentador";
                        fieldArray[2] = "Data";
                        fieldArray[3] = "Hora Início";
                        fieldArray[4] = "Hora Fim";
                        fieldArray[5] = "Ibope";
                    }
                    jtbView.setModel( new DefaultTableModel(s,fieldArray));
                } catch (SQLException ex) {
                    frame.printException(ex);
                    JOptionPane.showMessageDialog(null, "Erro ao tentar visualizar os dados.");
                }
                
            }
            
        }
        
    }

    
}
